﻿namespace Sustainalytics.Portfolios.Servive.Tests
{
    using System;
    using System.Net.Http;
    using System.Web.Http.Controllers;
    using System.Web.Http.Dispatcher;

    using FakeItEasy;
    using Sustainalytics.OAuth.Services.Common;
    using Sustainalytics.Portfolios.BL;
    using Sustainalytics.Portfolios.BL.Exceptions;
    using Sustainalytics.Portfolios.Service.Controllers;
    using Sustainalytics.Portfolios.Service.Models;
    using Sustainalytics.Utils;

    public class TestCompositionRoot : IHttpControllerActivator
    {
        public IHttpController Create(
            HttpRequestMessage request, 
            HttpControllerDescriptor controllerDescriptor, 
            Type controllerType)
        {
            var portfolioreader = A.Fake<IPortfolioReadHandler>();

            var portfolioRenameHandler = A.Fake<IPortfolioRenameHandler>();
            A.CallTo(() => portfolioRenameHandler.UpdatePortfolioName(A<Guid>._, A<string>._, A<PortfolioUserClaims>._))
                .Throws(() => new PortfolioModificationUnauthorizedException());

            var portfolioClaimsProcessor = A.Fake<PortfolioUserClaimsProcessor>();
            A.CallTo(() => portfolioClaimsProcessor.GetPortfolioClaims()).ReturnsLazily(x => new PortfolioUserClaims());

            if (controllerType == typeof(PortfoliosController))
            {
                var controller = new PortfoliosController(
                    portfolioreader, 
                    A.Fake<PortfolioDtoCreator>(), 
                    A.Fake<ICustomPortfolioUpdateHandler>(), 
                    portfolioRenameHandler, 
                    A.Fake<IPortfolioDeleteHandler>(), 
                    portfolioClaimsProcessor);
                return controller;
            }

            return null;
        }
    }
}